const _ = require('underscore');
const {
	Backend,
	Joi
} = require('dx-router-admin');

module.exports = class  Candidate extends Backend {
	constructor(ctx) {
		super(ctx);
		this.multiFields = [];
		this.indexWith = ['talent'];
		this.dto = {
			name: Joi.string().required().error(new Error('姓名不能为空'))
		};
	}
	async index(param){
		let talent_name = '';
		let statusList = [];
		let targetList = [];
		if(param.status){
			statusList = param.status;
			delete param.status;
		}
		if(param.target_names){
			targetList = param.target_names;
			delete param.target_names;
		}
		if(param.talent_name){
			talent_name = param.talent_name;
			delete param.talent_name;
		}
		param.size = param.size||1000;
		const result =  await super.index(param);
		let list = result.data.list;
		if(!_.isEmpty(statusList)){
			list = list.filter(candi=>{
				const promot_list = candi.promot_list;
				if(!promot_list||promot_list.length==0){
					return false;
				}
				const status = promot_list[promot_list.length-1]?.status;
				return statusList.includes(status);
			})
		}
		if(!_.isEmpty(targetList)){
			list = list.filter(candi=>{
				return targetList.includes(candi.target_name);
			});
		}
		if(talent_name){
		
			list = list.filter(candi=>{
				if(!candi.talent_name)return false;
				return talent_name.includes(candi.talent_name)||candi.talent_name.includes(talent_name);
			});
		}
		return this.$dx.success('操作成功',list);
	}
	join(param){
		return this.currentService.joinCandidate(param);
	}
	async getByProjectId(param){
		return await this.currentService.getByProjectId(param);
	}
	promot(param){
		return this.currentService.addPromot(param);
	}
};